package io.renren.modules.generator.service.impl;

import io.renren.modules.generator.entity.DeviceSiteEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.generator.dao.CodeDicDao;
import io.renren.modules.generator.entity.CodeDicEntity;
import io.renren.modules.generator.service.CodeDicService;


@Service("codeDicService")
public class CodeDicServiceImpl extends ServiceImpl<CodeDicDao, CodeDicEntity> implements CodeDicService {

    @Autowired
    CodeDicDao codeDicDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,CodeDicEntity codeDicEntity) {
        QueryWrapper<CodeDicEntity> wrapper = new QueryWrapper<>();
        if(Objects.nonNull(codeDicEntity.getType())){
            wrapper.eq("type",codeDicEntity.getType());
        }
        if(Objects.nonNull(codeDicEntity.getName())){
            wrapper.like("name",codeDicEntity.getName());
        }
        if(Objects.nonNull(codeDicEntity.getPid())){
            wrapper.like("pid",codeDicEntity.getPid());
        }else{
            wrapper.isNull("pid");
        }
        wrapper.orderByAsc("sort");
        IPage<CodeDicEntity> page = this.page(
                new Query<CodeDicEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<CodeDicEntity> selectAll(CodeDicEntity codeDicEntity) {
        QueryWrapper<CodeDicEntity> wrapper = new QueryWrapper<>();
        if(Objects.nonNull(codeDicEntity.getType())){
            wrapper.eq("type",codeDicEntity.getType());
        }
        if(Objects.nonNull(codeDicEntity.getName())){
            wrapper.like("name",codeDicEntity.getName());
        }
        if(Objects.nonNull(codeDicEntity.getPid())){
            wrapper.like("pid",codeDicEntity.getPid());
        }else{
            wrapper.isNull("pid");
        }
        wrapper.orderByAsc("sort");
        List<CodeDicEntity> codeDicEntityList = codeDicDao.selectList(wrapper);
        return codeDicEntityList;
    }

}